package com.starbaba.module.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import com.base.common.database.BaseDao;
import com.base.common.exception.AppException;
import com.starbaba.core.web.page.PageResponse;
import com.starbaba.loanActivity.model.UserCommentModel;
import com.starbaba.model.dictionary.Constant;
import com.starbaba.module.model.ActivityModel;
import com.starbaba.module.model.LoanContentActivity;
import com.starbaba.module.model.LoanContentActivityVars;
import com.starbaba.module.service.INewModelService;
import com.tuanzi.server.loan_base.model.db.activity.loan_content_activity_vars;

@Component
@SuppressWarnings("rawtypes")
public class NewModelServiceImpl implements INewModelService {

	@SuppressWarnings("deprecation")
    static BaseDao dao = new BaseDao(Constant.POOL_LIFE);

    @Override
	public List<ActivityModel> getDataList(int iDisplayStart, int iDisplayLength, PageResponse pageResponse, Map<String, Object> paramsMap) {

		String modelName = (String) paramsMap.get("modelName");
		String modelType = (String) paramsMap.get("modelType");

		StringBuffer sf = new StringBuffer();
		StringBuffer tt = new StringBuffer();
		sf.append("select * From activity_model as c where 1=1 ");

		if (modelType != null && modelType != "") {
			tt.append(" and  c.modelType = " + modelType);
		}
		if (modelName != null && modelName != "") {
			tt.append(" and  c.modelName like '%" + modelName + "%'");
		}

		sf.append(tt.toString());
		sf.append(" order by c.ctime desc LIMIT " + iDisplayStart + "," + iDisplayLength);
		List<ActivityModel> list = null;
		try {
			list = dao.list(sf.toString(), ActivityModel.class, new Object[] {});

			String countHql = "select count(*) from activity_model as c where 1=1 " + tt.toString();
			int count = dao.getInt(countHql, new Object[] {});

			pageResponse.setITotalRecords(count);
			pageResponse.setITotalDisplayRecords(count);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
    
    public void delActivity(String activityId) {
    	if (!StringUtils.isEmpty(activityId)) {
    		try {
				String delActivitySql = "DELETE FROM loan.loan_content_activity WHERE id = ? ";
				String delActivityVarsSql = "DELETE FROM loan.loan_content_activity_vars WHERE aid = ? ";
				String delActivityCommentSql = "DELETE FROM loan_user_comment WHERE activity_id = ? ";
				dao.delete(delActivitySql, new Object[]{activityId});
				dao.delete(delActivityVarsSql, new Object[]{activityId});
				dao.delete(delActivityCommentSql, new Object[]{activityId});
			} catch (AppException e) {
				e.printStackTrace();
			}
    	}
    	
    }

	@Override
	public void copyModel(String userId, String userName ,String oldActivityId) {
		try {
			if (!StringUtils.isEmpty(oldActivityId)) {
				// 活动内容
				String activitySql = "select * from loan_content_activity where id = ?";
				LoanContentActivity activity = dao.get(activitySql, LoanContentActivity.class, oldActivityId);

				// 活动模板项对应的值
				String activityVars = "select content,val from loan_content_activity_vars where aid =?";
				List<loan_content_activity_vars> contentList = dao.list(activityVars,loan_content_activity_vars.class, oldActivityId);
				
				// 活动评论
				String commSql = "SELECT * FROM loan_user_comment c WHERE c.activity_id = ? AND c.comment_type = 2 ";
				List<UserCommentModel> commList = dao.list(commSql, UserCommentModel.class, oldActivityId);

				LoanContentActivity newActivity = new LoanContentActivity();
				newActivity.setCtime(new Date());
				newActivity.setName(activity.getName()+"_副本");
				newActivity.setMemo(activity.getMemo());
				newActivity.setMid(activity.getMid());
				newActivity.setTarget_url(activity.getTarget_url());
				Long newActivityId = dao.addReturnGeneratedKey(newActivity);

				if(contentList != null && contentList.size() != 0) {
					for (loan_content_activity_vars content : contentList) {
						content.setId(null);
						content.setCtime(new Date());
						content.setAid(newActivityId);
						dao.add(content);
					}
				}
				
				if(commList != null && commList.size() != 0) {
					SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd");
					SimpleDateFormat sdfTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					Date currDate = new Date();
					for (UserCommentModel comment : commList) {
						comment.setId(null);
						comment.setUser_id(userId);
						comment.setUser_name(userName);
						comment.setActivity_id(newActivityId);
						comment.setCreate_time(sdfTime.format(currDate));
						comment.setCreate_date(sdfDate.format(currDate));
						comment.setUpdate_time(currDate);
						dao.add(comment);
					}
				}
				
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
